package com.keke.myblogv1.article.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.keke.myblogv1.article.dto.QueryArticleByPageDTO;
import com.keke.myblogv1.article.entity.MyBlogArticle;
import com.keke.myblogv1.article.mapper.MyBlogArticleMapper;
import com.keke.myblogv1.article.service.MyBlogArticleService;
import com.keke.myblogv1.common.result.ResponseResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Derrick
 * @since 2022-07-31
 */
@RestController
@RequestMapping("/article/")
public class MyBlogArticleController {

    @Resource
    private MyBlogArticleService myBlogArticleService;

    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResponseResult<Page<MyBlogArticle>> getArticleList(QueryArticleByPageDTO queryArticleByPageDTO) {
        Page<MyBlogArticle> allArticleByPage = null;
        try {
            allArticleByPage = myBlogArticleService.getAllArticleByPage(queryArticleByPageDTO);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error();
        }
        return ResponseResult.success(allArticleByPage);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public ResponseResult<MyBlogArticle> getArticleById(@PathVariable("id") int id) {
        MyBlogArticle allArticleById = null;
        try {
            allArticleById = myBlogArticleService.getAllArticleById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error();
        }
        return ResponseResult.success(allArticleById);
    }

    @RequestMapping(value = "/create",method = RequestMethod.POST)
    public ResponseResult insertArticle(@RequestBody MyBlogArticle myBlogArticle){
        try{
            myBlogArticleService.insertArticle(myBlogArticle);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error();
        }
        return ResponseResult.success();
    }
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public ResponseResult updateArticle(@RequestBody MyBlogArticle myBlogArticle){
        try{
            myBlogArticleService.updateArticle(myBlogArticle);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error();
        }
        return ResponseResult.success();
    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public ResponseResult deleteArticle(@RequestBody MyBlogArticle myBlogArticle){
        try{
            myBlogArticleService.deleteArticle(myBlogArticle);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error();
        }
        return ResponseResult.success();
    }
}
